package org.aceor.mddal.gateway.merger.groupby;

import com.google.common.base.Objects;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxue on 16/4/16.
 */
public final class GroupByKey {

    private final List<String> unionKeys = new ArrayList<>();

    public void append(final String key) {
        unionKeys.add(key);
    }

    @Override
    public int hashCode() {
        int result = 1;
        for (String key : unionKeys) {
            result = result * 31 + unionKeys.hashCode();
        }
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof GroupByKey)) {
            return false;
        }
        GroupByKey groupByKey = (GroupByKey) obj;
        if (groupByKey.unionKeys.size() != this.unionKeys.size()) {
            return false;
        }
        int len = unionKeys.size();
        for (int i = 0; i < len; i++) {
            if (!Objects.equal(groupByKey.unionKeys.get(i), this.unionKeys.get(i))) {
                return false;
            }
        }
        return true;
    }
}
